package com.ss.android.article.base.ui;

import android.annotation.TargetApi;
import android.content.Context;
import android.database.DataSetObservable;
import android.database.DataSetObserver;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.WrapperListAdapter;

import com.ss.android.article.browser.R;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Iterator;

public class GridViewWithHeaderAndFooter extends GridView {
    public static final class a {
        public View a;
        public ViewGroup b;
        public boolean c;

        public a(byte arg1) {
            this();
        }

        private a() {

        }
    }

    public static final class b extends FrameLayout {
        private final GridViewWithHeaderAndFooter a;

        public b(GridViewWithHeaderAndFooter arg1, Context arg2) {
            super(arg2);
            this.a = arg1;
        }

        protected final void onLayout(boolean arg3, int arg4, int arg5, int arg6, int arg7) {
            int v0 = this.a.getPaddingLeft() + this.getPaddingLeft();
            if (v0 != arg4) {
                this.offsetLeftAndRight(v0 - arg4);
            }

            super.onLayout(arg3, arg4, arg5, arg6, arg7);
        }

        protected final void onMeasure(int arg3, int arg4) {
            super.onMeasure(View.MeasureSpec.makeMeasureSpec(this.a.getMeasuredWidth() - this.a.getPaddingLeft() - this.a.getPaddingRight(), View.MeasureSpec.getMode(arg3)), arg4);
        }
    }

    public static final class c implements Filterable, WrapperListAdapter {
        public final DataSetObservable a;
        int b;
        private final ListAdapter c;
        private static ArrayList<a> d = new ArrayList();
        private ArrayList<a> e;
        private ArrayList<a> f;
        private int g;
        private boolean h;
        private final boolean i;
        private boolean j;

        public c(ArrayList arg3, ArrayList arg4, ListAdapter arg5) {
            boolean v0 = true;

            this.a = new DataSetObservable();
            this.g = 1;
            this.b = -1;
            this.j = true;
            this.c = arg5;
            this.i = arg5 instanceof Filterable;
            this.e = arg3 == null ? d : arg3;
            this.f = arg4 == null ? d : arg4;
            if (!a(this.e) || !a(this.f)) {
                v0 = false;
            }

            this.h = v0;
        }

        public final void a(int arg2) {
            if (arg2 > 0 && this.g != arg2) {
                this.g = arg2;
                this.a.notifyChanged();
            }
        }

        private static boolean a(ArrayList<a> arg2) {
            boolean v0;
            if (arg2 != null) {
                Iterator v1 = arg2.iterator();
                do {
                    if (v1.hasNext()) {
                        if (((a) v1.next()).c) {
                            continue;
                        }

                        v0 = false;
                        break;
                    } else {
                        v0 = true;
                        break;
                    }
                }
                while (true);
            } else {
                label_9:
                v0 = true;
            }

            return v0;
        }

        private int a() {
            return ((int) (Math.ceil(((double) (1f * (((float) this.c.getCount())) / (((float) this.g))))) * (((double) this.g))));
        }

        public final boolean areAllItemsEnabled() {
            boolean v0;
            if (this.c != null) {
                if ((this.h) && (this.c.areAllItemsEnabled())) {
                    v0 = true;
                } else {
                    v0 = false;
                }
            } else {
                label_7:
                v0 = true;
            }

            return v0;
        }

        public final int getCount() {
            int v0 = this.c != null ? (this.f.size() + this.e.size()) * this.g + this.a() : (this.f.size() + this.e.size()) * this.g;
            return v0;
        }

        public final Filter getFilter() {
            Filter v0 = this.i ? ((Filterable) this.c).getFilter() : null;
            return v0;
        }

        public final Object getItem(int arg5) {
            Object v0_1;
            Object v1 = null;
            int v0 = this.e.size() * this.g;
            if (arg5 >= v0) {
                int v2 = arg5 - v0;
                v0 = 0;
                if (this.c != null) {
                    v0 = this.a();
                    if (v2 < v0) {
                        return v2 < this.c.getCount() ? this.c.getItem(v2) : v1;
                    }
                }

                v0 = v2 - v0;
                if (v0 % this.g == 0) {
                    this.f.get(v0);
                    return v1;
                }

                v0_1 = v1;
            } else if (arg5 % this.g == 0) {
                this.e.get(arg5 / this.g);
                v0_1 = v1;
            } else {
                v0_1 = v1;
            }

            return v0_1;
        }

        public final long getItemId(int arg3) {
            long v0_1;
            int v0 = this.e.size() * this.g;
            if (this.c == null || arg3 < v0) {
                label_14:
                v0_1 = -1;
            } else {
                v0 = arg3 - v0;
                if (v0 < this.c.getCount()) {
                    v0_1 = this.c.getItemId(v0);
                } else {
                    v0_1 = -1;
                }
            }

            return v0_1;
        }

        public final int getItemViewType(int arg6) {
            int v1 = 0;
            int v3 = this.e.size() * this.g;
            int v0 = this.c == null ? 0 : this.c.getViewTypeCount() - 1;
            int v2 = -2;
            if ((this.j) && arg6 < v3 && arg6 % this.g != 0) {
                v2 = arg6 / this.g + 1 + v0;
            }

            v3 = arg6 - v3;
            if (this.c != null) {
                v1 = this.a();
                if (v3 >= 0 && v3 < v1) {
                    if (v3 < this.c.getCount()) {
                        v2 = this.c.getItemViewType(v3);
                    } else if (this.j) {
                        v2 = this.e.size() + v0 + 1;
                    }
                }
            }

            if (this.j) {
                v1 = v3 - v1;
                if (v1 >= 0 && v1 < this.getCount() && v1 % this.g != 0) {
                    v2 = v0 + this.e.size() + 1 + (v1 / this.g + 1);
                }
            }

            return v2;
        }

        public final View getView(int position, View convertView, ViewGroup parent) {
            ViewGroup v0_1;
            int v3 = 4;
            int v0 = this.e.size() * this.g;
            if (position < v0) {
                v0 = position / this.g;
                if (v0 >= 0 && v0 < this.e.size()) {
                    v0_1 = this.e.get(v0).b;
                    if (position % this.g == 0) {
                        convertView = v0_1;
                    } else {
                        if (convertView == null) {
                            convertView = new View(parent.getContext());
                        }
                        convertView.setVisibility(INVISIBLE);
                        convertView.setMinimumHeight(v0_1.getHeight());
                    }
                }
            } else {
                int v1 = position - v0;
                v0 = 0;
                if (this.c != null) {
                    v0 = this.a();
                    if (v1 < v0) {
                        if (v1 < this.c.getCount()) {
                            convertView = this.c.getView(v1, convertView, parent);
                        } else {
                            if (convertView == null) {
                                convertView = new View(parent.getContext());
                            }
                            convertView.setVisibility(INVISIBLE);
                            convertView.setMinimumHeight(this.b);
                        }
                        return convertView;
                    }
                }

                v0 = v1 - v0;
                if (v0 >= this.getCount()) {
                    throw new ArrayIndexOutOfBoundsException(position);
                }

                v0_1 = this.f.get(v0 / this.g).b;
                if (position % this.g == 0) {
                    convertView = v0_1;
                    return convertView;
                }

                if (convertView == null) {
                    convertView = new View(parent.getContext());
                }

                convertView.setVisibility(INVISIBLE);
                convertView.setMinimumHeight(v0_1.getHeight());
            }

            return convertView;

        }

        public final int getViewTypeCount() {
            int v0 = this.c == null ? 1 : this.c.getViewTypeCount();
            if (this.j) {
                v0 += this.e.size() + 1 + this.f.size();
            }
            return v0;
        }

        public final ListAdapter getWrappedAdapter() {
            return this.c;
        }

        public final boolean hasStableIds() {
            return this.c != null && this.c.hasStableIds();
        }

        public final boolean isEmpty() {
            return this.c == null || (this.c.isEmpty());
        }

        public final boolean isEnabled(int arg6) {
            boolean v1 = false;
            int v0 = this.e.size() * this.g;
            if (arg6 >= v0) {
                int v3 = arg6 - v0;
                if (this.c != null) {
                    v0 = this.a();
                    if (v3 < v0) {
                        if (v3 >= this.c.getCount()) {
                        } else if (this.c.isEnabled(v3)) {
                            v1 = true;
                        } else {
                        }

                        return v1;
                    }
                } else {
                    v0 = 0;
                }

                v0 = v3 - v0;
                if (v0 % this.g != 0) {
                    return v1;
                }

                if (!this.f.get(v0 / this.g).c) {
                    return v1;
                }

                v1 = true;
            } else if (arg6 % this.g == 0 && (this.e.get(arg6 / this.g).c)) {
                v1 = true;
            }

            return v1;
        }

        public final void registerDataSetObserver(DataSetObserver arg2) {
            this.a.registerObserver(arg2);
            if (this.c != null) {
                this.c.registerDataSetObserver(arg2);
            }
        }

        public final void unregisterDataSetObserver(DataSetObserver arg2) {
            this.a.unregisterObserver(arg2);
            if (this.c != null) {
                this.c.unregisterDataSetObserver(arg2);
            }
        }
    }

    final class d implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {
        private final GridViewWithHeaderAndFooter a;

        d(GridViewWithHeaderAndFooter arg1, byte arg2) {
            this(arg1);
        }

        private d(GridViewWithHeaderAndFooter arg1) {
            this.a = arg1;

        }

        public final void onItemClick(AdapterView arg7, View arg8, int arg9, long arg10) {
            if (this.a.a != null) {
                int v3 = arg9 - this.a.getHeaderViewCount() * this.a.getNumColumnsCompatible();
                if (v3 >= 0) {
                    this.a.a.onItemClick(arg7, arg8, v3, arg10);
                }
            }
        }

        public final boolean onItemLongClick(AdapterView arg7, View arg8, int arg9, long arg10) {
            if (this.a.b != null) {
                int v3 = arg9 - this.a.getHeaderViewCount() * this.a.getNumColumnsCompatible();
                if (v3 >= 0) {
                    this.a.b.onItemLongClick(arg7, arg8, v3, arg10);
                }
            }

            return true;
        }
    }

    AdapterView.OnItemClickListener a;
    AdapterView.OnItemLongClickListener b;
    public ArrayList<a> c;
    public ArrayList<a> d;
    private View e;
    private int f;
    private ListAdapter g;
    private d h;

    public GridViewWithHeaderAndFooter(Context arg2) {
        super(arg2);
        this.e = null;
        this.f = -1;
        this.c = new ArrayList();
        this.d = new ArrayList();
    }

    public GridViewWithHeaderAndFooter(Context arg2, AttributeSet arg3) {
        super(arg2, arg3);
        this.e = null;
        this.f = -1;
        this.c = new ArrayList();
        this.d = new ArrayList();
    }

    public GridViewWithHeaderAndFooter(Context context, AttributeSet arg3, int arg4) {
        super(context, arg3, arg4);
        this.e = null;
        this.f = -1;
        this.c = new ArrayList();
        this.d = new ArrayList();
    }

    @TargetApi(value = 16)
    private int getColumnWidthCompatible() {
        int v0;
        if (Build.VERSION.SDK_INT >= 16) {
            v0 = super.getColumnWidth();
        } else {
            try {
                Field v0_3 = GridView.class.getDeclaredField("mColumnWidth");
                v0_3.setAccessible(true);
                v0 = v0_3.getInt(this);
            } catch (IllegalAccessException v0_1) {
                throw new RuntimeException(((Throwable) v0_1));
            } catch (NoSuchFieldException v0_2) {
                throw new RuntimeException(((Throwable) v0_2));
            }
        }

        return v0;
    }

    public int getFooterViewCount() {
        return this.d.size();
    }

    public int getHeaderViewCount() {
        return this.c.size();
    }

    @TargetApi(value = 16)
    public int getHorizontalSpacing() {
        int v0 = 0;
        try {
            if (Build.VERSION.SDK_INT < 16) {
                Field v1_1 = GridView.class.getDeclaredField("mHorizontalSpacing");
                v1_1.setAccessible(true);
                return v1_1.getInt(this);
            }

            v0 = super.getHorizontalSpacing();
        } catch (Exception v1) {
        }

        return v0;
    }

    private d getItemClickHandler() {
        if (this.h == null) {
            this.h = new d(this, (byte) 0);
        }

        return this.h;
    }

    int getNumColumnsCompatible() {
        return super.getNumColumns();
    }

    public ListAdapter getOriginalAdapter() {
        return this.g;
    }

    public int getRowHeight() {
        int v0;
        int v2 = -1;
        if (this.f > 0) {
            v0 = this.f;
        } else {
            ListAdapter v0_1 = this.getAdapter();
            int v3 = this.getNumColumnsCompatible();
            if (v0_1 != null && v0_1.getCount() > (this.c.size() + this.d.size()) * v3) {
                int v4 = this.getColumnWidthCompatible();
                View v3_1 = this.getAdapter().getView(v3 * this.c.size(), this.e, ((ViewGroup) this));
                if (v3_1 == null) {
                    v0 = 0;
                } else {
                    ViewGroup.LayoutParams v0_2 = v3_1.getLayoutParams();
                    if (v0_2 == null) {
                        v0_2 = new AbsListView.LayoutParams(v2, -2, 0);
                        v3_1.setLayoutParams(v0_2);
                    }

                    v3_1.measure(GridViewWithHeaderAndFooter.getChildMeasureSpec(View.MeasureSpec.makeMeasureSpec(v4, MeasureSpec.EXACTLY), 0, ((AbsListView.LayoutParams) v0_2).width), GridViewWithHeaderAndFooter.getChildMeasureSpec(View.MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 0, ((AbsListView.LayoutParams) v0_2).height));
                    this.e = v3_1;
                    this.f = v3_1.getMeasuredHeight();
                    v0 = this.f;
                }

                return v0;
            }

            v0 = v2;
        }

        return v0;
    }

    @TargetApi(value = 16)
    public int getVerticalSpacing() {
        int v0 = 0;
        try {
            if (Build.VERSION.SDK_INT < 16) {
                Field v1_1 = GridView.class.getDeclaredField("mVerticalSpacing");
                v1_1.setAccessible(true);
                return v1_1.getInt(this);
            }

            v0 = super.getVerticalSpacing();
        } catch (Exception v1) {
        }

        return v0;
    }

    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        this.e = null;
    }

    protected void onMeasure(int arg11, int arg12) {
        int v2 = 0;
        super.onMeasure(arg11, arg12);
        ListAdapter v1 = this.getAdapter();
        if (v1 != null && ((v1 instanceof c))) {
            ((c) v1).a(this.getNumColumnsCompatible());
            ((c) v1).b = this.getRowHeight();
        }

        int v4 = View.MeasureSpec.getSize(arg11);
        int v5 = this.getNumColumnsCompatible();
        int v6 = this.getRowHeight();
        int v7 = this.getResources().getDimensionPixelOffset(R.dimen.pic_detail_related_picture_vertical_gap);
        int v1_1 = this.g == null ? 0 : this.g.getCount();
        int v3 = View.MeasureSpec.getSize(arg12);
        int v8 = this.getPaddingBottom() + v7 + (this.c.get(0).a.getMeasuredHeight() * (this.getFooterViewCount() + this.getHeaderViewCount()) + this.getPaddingTop() + v7);
        int v0 = v3 - v8;
        for (v3 = 0; v3 < v1_1; v3 += v5) {
            v2 += v6;
            if (v3 + v5 < v1_1) {
                v2 += v7;
            }

            if (v2 >= v0) {
                this.setMeasuredDimension(v4, v0 + v8);
                return;
            }
        }

        v0 = v2;
        label_43:
        this.setMeasuredDimension(v4, v0 + v8);
    }

    public void setAdapter(ListAdapter arg4) {
        this.g = arg4;
        if (this.c.size() > 0 || this.d.size() > 0) {
            c v0 = new c(this.c, this.d, arg4);
            int v1 = this.getNumColumnsCompatible();
            if (v1 > 1) {
                v0.a(v1);
            }

            v0.b = this.getRowHeight();
            super.setAdapter(v0);
        } else {
            super.setAdapter(arg4);
        }
    }

    public void setClipChildren(boolean arg1) {
    }

    public void setClipChildrenSupper(boolean arg2) {
        super.setClipChildren(false);
    }

    public void setNumColumns(int arg3) {
        super.setNumColumns(arg3);
        ListAdapter v0 = this.getAdapter();
        if (v0 != null && ((v0 instanceof c))) {
            ((c) v0).a(arg3);
        }
    }

    public void setOnItemClickListener(AdapterView.OnItemClickListener arg2) {
        this.a = arg2;
        super.setOnItemClickListener(this.getItemClickHandler());
    }

    public void setOnItemLongClickListener(AdapterView.OnItemLongClickListener arg2) {
        this.b = arg2;
        super.setOnItemLongClickListener(this.getItemClickHandler());
    }
}

